﻿@model Xms.Web.Models.EditEntityPermissionModel

<div class="panel panel-default">
    <div class="panel-heading">
        <h3 class="panel-title">
            <a data-toggle="collapse"
               href="#collapseTwo">
                <strong>@app.PageTitle</strong>
            </a>
        </h3>
    </div>
    <div id="collapseTwo" class="panel-collapse collapse in">
        <div class="panel-body">
            @using (Html.BeginForm("EditEntityPermission", "security", FormMethod.Post, new { @id = "editform", @class = "form-horizontal", @data_autoreset = (Model.EntityPermissionId.HasValue && !Model.EntityPermissionId.Equals(Guid.Empty) ? "" : "true") }))
            {
                @Html.AntiForgeryToken()
                @Html.HiddenFor(x => x.EntityPermissionId)
                <div class="form-group col-sm-12">
                    @Html.LabelFor(x => x.Name, "名称", new { @class = "col-sm-2 control-label" })
                    <div class="col-sm-10">
                        @Html.TextBoxFor(x => x.Name, new { @class = "form-control" })
                    </div>
                </div>
                <div class="form-group col-sm-12">
                    @Html.LabelFor(x => x.EntityName, "实体", new { @class = "col-sm-2 control-label" })
                    <div class="col-sm-10">
                        <div class="typeahead__container">
                            <div class="typeahead__field input-group">

                                <span class="typeahead__query">
                                    @Html.TextBoxFor(x => x.EntityName, new { @id = "EntityName", @autocomplete = "off", @class = "form-control typeahead required" })
                                    @Html.HiddenFor(x => x.EntityId, new { @class = "form-control required" })
                                </span>
                                <span class="input-group-btn">
                                    <button class="btn btn-default btn-sm" type="button" onclick="Xms.Web.OpenDialog('/tool/SelectEntitiesDialog?singlemode=true', 'bindSelectedEntity')">
                                        <span class="glyphicon glyphicon-search"></span>
                                    </button>
                                </span>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="form-group col-sm-12">
                    @Html.LabelFor(x => x.Description, "操作类型", new { @class = "col-sm-2 control-label" })
                    <div class="col-sm-10">
                        @Html.DropDownListFor(x => x.AccessRight, Model.PermissionTypes, new { @class = "form-control" })
                    </div>
                </div>
                <div class="form-group col-sm-12">
                    @Html.LabelFor(x => x.State, "状态", new { @class = "col-sm-2 control-label" })
                    <div class="col-sm-10">
                        <label class="checkbox-inline">
                            @Html.RadioButtonFor(x => x.State, Xms.Core.RecordState.Enabled, new { @class = "required" }) 可用
                        </label>
                        <label class="checkbox-inline">
                            @Html.RadioButtonFor(x => x.State, Xms.Core.RecordState.Disabled, new { @class = "required" }) 禁用
                        </label>
                    </div>
                </div>
                <div class="form-group col-sm-12">
                    @Html.LabelFor(x => x.Description, "描述", new { @class = "col-sm-2 control-label" })
                    <div class="col-sm-10">
                        @Html.TextAreaFor(x => x.Description, new { @class = "form-control", @rows = "3" })
                    </div>
                </div>
                <div class="form-group col-sm-12 text-center" id="form-buttons">
                    <button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-saved"></span> 保存</button>
                    <button type="reset" class="btn btn-default"><span class="glyphicon glyphicon-refresh"></span> 重置</button>
                </div>
            }
        </div>
    </div>
</div>
@section Header{
    <link rel="stylesheet" href="/content/js/jquery.typeahead.min.css?v=@app.PlatformSettings.VersionNumber">
}
@section Scripts {
    <script src="/content/js/jquery.typeahead.min.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script src="/content/js/jquery.form.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script src="/content/js/jquery-validate/jquery.validate.min.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script src="/content/js/jquery-validate/localization/messages_zh.min.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script>
        $(function () {
            //表单验证
            Xms.Web.Form($("#editform"));

            initEntityAutoComplete();
        });
        function initEntityAutoComplete() {
            $.typeahead({
                input: "#EntityName",
                minLength: 1,
                maxItem: 5,
                order: "asc",
                dynamic: true,
                highlight: true,
                cancelButton: true,
                //searchOnFocus: true,
                emptyTemplate: '没有关于 "{{query}}" 的记录',
                delay: 300,
                display: ["localizedname", "name"],
                template: '<span>{{localizedname}} - {{name}}</span>',
                source: {
                    url: [{
                        type: "GET",
                        url: ORG_SERVERURL + "/api/schema/entity",
                        callback: {
                            done: function (data, textStatus, jqXHR) {
                                console.log(data);
                                // IMPORTANT: data has to be returned
                                return Xms.Web.GetAjaxResult(data).content.items;
                            },
                            fail: function (jqXHR, textStatus, errorThrown) { },
                            complete: function (jqXHR, textStatus) { },
                            always: function (data, textStatus, jqXHR) { }
                        }
                    }]
                },
                callback: {
                    onClickAfter: function (node, a, item, event) {
                        console.log(node, a, item, event);
                        $('#EntityId').val(item.entityid);
                        node.val(item.name);
                        if ($('#Name').val() == '') {
                            $('#Name').val(item.name.toLowerCase() + '.').focus();
                        }
                        if ($('#Description').val() == '') {
                            $('#Description').val(item.localizedname);
                        }
                    }
                }
            });
        }
        function bindSelectedEntity(data) {
            $('#EntityId').val(data[0].id);
            $('#EntityName').val(data[0].name);
        }
    </script>
}